package sorting;

import java.util.Arrays;
import java.util.Random;

public class BinarySearch {
	static int bs(int[] a, int key) {
		int low = 0;
		int high = a.length - 1;
		while (low <= high) {
			int mid = (high+low)>>1;//low + (high - low) >> 1;
			if (a[mid] == key)
				return mid;
			else if (a[mid] < key)
				low = mid + 1;
			else
				high = mid - 1;
		}
		return -1;
	}

	public static void main(String[] args) {
		int size = 10;
		Random rand = new Random();
		int[] a = new int[size];
		for (int i = 0; i < size; i++)
			a[i] = rand.nextInt(20);
		Arrays.sort(a);
		System.out.println(Arrays.toString(a));
		int key = 15;
		System.out.println(Arrays.binarySearch(a, key));
		System.out.println(bs(a, key));;
		
	}
}
